import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { SGuildAltarBoxReward } from "../../../Message/Game/SGuildAltarBoxReward";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { DataEvent } from "../events/DataEvent";
import { ENpcEffect } from "../../../Message/Public/ENpcEffect";
import { AIManager } from "../scene/ai/AIManager";
import { AIType } from "../scene/ai/AIType";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { SGuildAltarInfo } from "../../../Message/Game/SGuildAltarInfo";
import { IView } from "../../mvc/interfaces/IView";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { GuildAnimalGetWindow } from "../view/guild/animal/GuildAnimalGetWindow";
import { GuildAnimalIcon } from "../view/guild/animal/GuildAnimalIcon";
import { GuildAnimalAlertWindow } from "../view/guild/animal/GuildAnimalAlertWindow";
import { GuildAnimalGetItemsWindow } from "../view/guild/animal/GuildAnimalGetItemsWindow";
import { GuildAnimalShowView } from "../view/guild/animal/GuildAnimalShowView";
import { GuildAltarConfig } from "../resource/GuildAltarConfig";
import { GameProxy } from "../mvc/GameProxy";
import { UiHintIconView } from "../view/uiIconBtn/UiHintIconView";
import { Alert } from "../../../com/mui/controls/Alert";
import { SPlayerGuildInfo } from "../../../Message/Game/SPlayerGuildInfo";
import { SGuildBeastInfo } from "../../../Message/Game/SGuildBeastInfo";
type int = number;
//class GuildAnimalController
    
    export  class GuildAnimalController extends Controller
    {
        constructor()
        {
            
            super();Dispatcher.addEventListener(EventName.GuildAnimalGetWindowOpen, this.onGuildAnimalGetWindowOpenHandler);
            Dispatcher.addEventListener(EventName.GuildAnimalGet, this.onGuildAnimalGetHandler);
            Dispatcher.addEventListener(EventName.GuildAnimalGetSuccess, this.onGuildAnimalGetSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildAnimalCall, this.onGuildAnimalCallHandler);
            Dispatcher.addEventListener(EventName.GuildAnimalCallSuccess, this.onGuildAnimalCallSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildAnimalFeed, this.onGuildAnimalFeedHandler);
            Dispatcher.addEventListener(EventName.GuildAnimalFeedSuccess, this.onGuildAnimalFeedSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildAnimalAlertWindowClose, this.onGuildAnimalAlertWindowCloseHandler);
            Dispatcher.addEventListener(EventName.GuildAnimalCopyEnter, this.onGuildAnimalCopyEnterHandler);
            Dispatcher.addEventListener(EventName.GuildAnimalCopyLeave, this.onGuildAnimalCopyLeaveHandler);
            Dispatcher.addEventListener(EventName.GuildAnimalGetItem, this.onGuildAnimalGetItemHandler);
            Dispatcher.addEventListener(EventName.GuildAnimalGetItemSuccess, this.onGuildAnimalGetItemSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildAnimalGotoNpc, this.onGuildAnimalGotoNpcHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            Dispatcher.addEventListener(EventName.StageResize, this.onStageResizeHandler);
            return;
        }

        /* internal  */onGuildAnimalBoxRewardCommandHandler(arg1: SGuildAltarBoxReward): void
        {
            var loc1=0;
            if (arg1) 
            {
                if (arg1.exp > 0) 
                {
                    loc1 = arg1.exp * cache.role.entityInfo.level;
                    MsgManager.showRollTipsMsg(Language.getStringByParam(57547, loc1));
                }
                if (arg1.rewardItems.length > 0) 
                {
                    this.getItemsWindow.updateByItems(arg1.rewardItems);
                    this.getItemsWindow.show();
                }
            }
            return;
        }

        /* internal  */onGuildAnimalGotoNpcHandler(arg1: DataEvent): void
        {
            var loc1=cache.scene.getNpcByEffect(ENpcEffect._ENpcEffectBeastShow);
            if (loc1) 
            {
                if (cache.guild.isInSelfManorMap()) 
                {
                    AIManager.onAutoPathAIControl(loc1);
                }
                else 
                {
                    AIManager.onAIControl(loc1, AIType.AI_Convey);
                }
            }
            return;
        }

        /* internal  */onSceneUpdateHandler(arg1: DataEvent): void
        {
            if (GameMapUtil.curMapState.isGuildAltar) 
            {
                this.showView.updateByType(cache.guildAnimal.bossType, cache.guildAnimal.bossId);
                this.showView.show();
            }
            else if (this._showView && !this._showView.isHide) 
            {
                this._showView.hide();
            }
            return;
        }

        /* internal  */onStageResizeHandler(arg1: DataEvent): void
        {
            if (this._showView && !this._showView.isHide) 
            {
                this._showView.resize();
            }
            return;
        }

        /* internal  */onGuildAltarInfoCommandHandler(arg1: SGuildAltarInfo): void
        {
            this.showView.updateByType(cache.guildAnimal.bossType, cache.guildAnimal.bossId);
            return;
        }

        public /* override */ get view(): IView
        {
            return this.initView();
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            NetDispatcher.addCmdListener(ServerCommand.GuildAnimalCallSuccessCommand, this.onGuildAnimalCallSuccessCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.GuildUpdateInfo, this.onGuildUpdateInfoHandler);
            NetDispatcher.addCmdListener(ServerCommand.GuildAnimalInfoCommand, this.onGuildAnimalInfoCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.GuildAnimalBoxRewardCommand, this.onGuildAnimalBoxRewardCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.GuildAltarInfoCommand, this.onGuildAltarInfoCommandHandler);
            return;
        }

        protected /* override */ initView(): IView
        {
            return null;
        }

        public get getWindow(): GuildAnimalGetWindow
        {
            if (this._getWindow == null) 
            {
                this._getWindow = new GuildAnimalGetWindow();
            }
            return this._getWindow;
        }

        public get animalIcon(): GuildAnimalIcon
        {
            if (!this._animalIcon) 
            {
                this._animalIcon = new GuildAnimalIcon();
                this._animalIcon.addEventListener(flash.events.MouseEvent.CLICK, this.onGuildAnimalIconClickHandler);
            }
            return this._animalIcon;
        }

        public get animalAlertWindow(): GuildAnimalAlertWindow
        {
            if (!this._animalAlertWindow) 
            {
                this._animalAlertWindow = new GuildAnimalAlertWindow();
            }
            return this._animalAlertWindow;
        }

        public get getItemsWindow(): GuildAnimalGetItemsWindow
        {
            if (!this._getItemsWindow) 
            {
                this._getItemsWindow = new GuildAnimalGetItemsWindow();
            }
            return this._getItemsWindow;
        }

        public get showView(): GuildAnimalShowView
        {
            if (!this._showView) 
            {
                this._showView = new GuildAnimalShowView();
            }
            return this._showView;
        }

        /* internal  */onGuildAnimalGetWindowOpenHandler(arg1: DataEvent): void
        {
            var loc1=cache.guild.getAnimalAltarLevel();
            var loc2=GuildAltarConfig.instance.getConfigByLevel(loc1);
            if (loc2) 
            {
                this.getWindow.updateByGuildAltar(loc2);
            }
            this.getWindow.updateGuildMoney(cache.guild.myGuildInfo.money);
            this.getWindow.show();
            return;
        }

        /* internal  */onGuildAnimalGetHandler(arg1: DataEvent): void
        {
            GameProxy.guildProxy.adoptGuildAltarBeast(arg1.data as int);
            return;
        }

        /* internal  */onGuildAnimalGetSuccessHandler(arg1: DataEvent): void
        {
            MsgManager.showRollTipsMsg(Language.getString(57525));
            if (this._getWindow) 
            {
                this._getWindow.hide();
            }
            return;
        }

        /* internal  */onGuildAnimalCallHandler(arg1: DataEvent): void
        {
            GameProxy.guildProxy.summonGuildAltarBeast();
            return;
        }

        /* internal  */onGuildAnimalCallSuccessHandler(arg1: DataEvent): void
        {
            MsgManager.showRollTipsMsg(Language.getString(57531));
            if (!this.animalIcon.parent) 
            {
                UiHintIconView.instance.addHintIcon(this.animalIcon);
            }
            return;
        }

        /* internal  */onGuildAnimalFeedHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["uid"] as string;
            var loc2=arg1.data["amount"] as int;
            GameProxy.guildProxy.feedGuildAltarBeast(loc1, loc2);
            return;
        }

        /* internal  */onGuildAnimalFeedSuccessHandler(arg1: DataEvent): void
        {
            MsgManager.showRollTipsMsg(Language.getString(57536));
            return;
        }

        /* internal  */onGuildAnimalIconClickHandler(arg1: Object /* flash.events.Event */): void
        {
            this.animalAlertWindow.show();
            return;
        }

        /* internal  */onGuildAnimalAlertWindowCloseHandler(arg1: DataEvent): void
        {
            var loc1=cache.guildAnimal.isOpened;
            if (loc1) 
            {
                if (!this.animalIcon.parent) 
                {
                    UiHintIconView.instance.addHintIcon(this.animalIcon);
                }
            }
            return;
        }

        /* internal  */onGuildAnimalCopyEnterHandler(arg1: DataEvent): void
        {
            var loc1=cache.guildAnimal.isCalled;
            if (!loc1) 
            {
                MsgManager.showRollTipsMsg(Language.getString(57541));
                return;
            }
            GameProxy.copy.enterGuildAltar();
            return;
        }

        /* internal  */onGuildAnimalCopyLeaveHandler(arg1: DataEvent): void
        {
            var e: DataEvent;
            var onBtnClickHandler: Function;

            var loc1;
            onBtnClickHandler = null;
            e = arg1;
            onBtnClickHandler = (arg1: int): void
            {
                if (arg1 == Alert.OK) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildEnterManor, {"guildId": cache.guild.myGuildInfo.guildId, "useContribution": false}));
                    Alert.resetToDefault();
                }
                return;
            }
            Alert.show(Language.getString(57545), null, Alert.OK | Alert.CANCEL, null, onBtnClickHandler);
            return;
        }

        /* internal  */onGuildAnimalGetItemHandler(arg1: DataEvent): void
        {
            GameProxy.guildProxy.getGuildBoxReward();
            return;
        }

        /* internal  */onGuildAnimalGetItemSuccessHandler(arg1: DataEvent): void
        {
            if (this._getItemsWindow) 
            {
                this._getItemsWindow.hide();
            }
            return;
        }

        /* internal  */onGuildAnimalCallSuccessCommandHandler(arg1: Object): void
        {
            return;
        }

        /* internal  */onGuildUpdateInfoHandler(arg1: SPlayerGuildInfo): void
        {
            if (arg1 && this._getWindow && this._getWindow.isHide == false) 
            {
                this._getWindow.updateGuildMoney(arg1.money);
            }
            return;
        }

        /* internal  */onGuildAnimalInfoCommandHandler(arg1: SGuildBeastInfo): void
        {
            var loc1=cache.guildAnimal.isKilled;
            if (loc1) 
            {
                if (this._animalIcon && this._animalIcon.parent) 
                {
                    UiHintIconView.instance.removeHintIcon(this._animalIcon);
                }
            }
            else 
            {
                UiHintIconView.instance.addHintIcon(this.animalIcon);
            }
            return;
        }

        private /* var */_getWindow: GuildAnimalGetWindow;

        private /* var */_animalIcon: GuildAnimalIcon;

        private /* var */_animalAlertWindow: GuildAnimalAlertWindow;

        private /* var */_getItemsWindow: GuildAnimalGetItemsWindow;

        private /* var */_showView: GuildAnimalShowView;
    }
